package com.bjx.generator.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import com.bjx.generator.domain.bo.TableFieldBO;
import com.bjx.generator.domain.entity.TableFieldEntity;
import com.bjx.generator.enumerate.FieldTypeEnum;
import com.bjx.generator.mapper.PostgreSqlCodeMapper;
import com.bjx.generator.service.IDatabaseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service("postgreSqlDatabaseServiceImpl")
public class PostgreSqlDatabaseServiceImpl implements IDatabaseService {
    @Autowired
    private PostgreSqlCodeMapper postgreSqlCodeMapper;
    @Override
    public List<String> listTableName(String schemaName) {
        List<String> list = postgreSqlCodeMapper.listTableName(schemaName);
        return list;
    }

    @Override
    public String selectTableComment(String tableName) {
        return postgreSqlCodeMapper.selectTableComment(tableName);
    }

    @Override
    public List<TableFieldEntity> selectTableFieldByTableName(String tableName) {
        List<TableFieldBO> tableFieldBOS = postgreSqlCodeMapper.selectTableFieldByTableName(tableName);
        List<TableFieldEntity> entityList = new ArrayList<TableFieldEntity>();
        if(CollectionUtil.isEmpty(tableFieldBOS)){
            return  entityList;
        }
        tableFieldBOS.forEach(res->{
            TableFieldEntity entity = Convert.convert(TableFieldEntity.class, res);
            entity.setFieldType(FieldTypeEnum.getFieldType(res.getFieldType()));
            entity.setPrimaryKey(res.getPrimaryKey() != null && res.getPrimaryKey()? (short)1 : (short)2);
            entity.setEmptyKey(res.getIsNullAble()? (short)2 : 1);
            entityList.add(entity);
        });
        return entityList;
    }
}
